package com.cskaoyan.advice;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.stereotype.Component;

/**
 * @author stone
 * @date 2022/08/29 16:30
 */
@Component
public class CountTimeAdvice implements MethodInterceptor {
    // invoke方法就是指导增强的方法，类似于InvocationHandler的invoke方法
    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        // 比如我们要计算方法的执行时间，那么我们来写一下业务
        long start = System.currentTimeMillis();
        // 执行委托类方法
        Object proceed = methodInvocation.proceed();
        long end = System.currentTimeMillis();
        System.out.println("方法的执行时间：" + (end - start));

        return proceed;
    }
}
